package samples;

import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;

public class Test {
	/** An adaptor that tells ANTLR to build CymbolAST nodes */
	public static TreeAdaptor modelAdaptor = new CommonTreeAdaptor() {
		public Object create(Token token) {
			return new ModelAST(token);
		}

		public Object dupNode(Object t) {
			if (t == null) {
				return null;
			}
			return create(((ModelAST) t).token);
		}

		public Object errorNode(TokenStream input, Token start, Token stop,
				RecognitionException e) {
			ModelErrorNode t = new ModelErrorNode(input, start, stop, e);
			// System.out.println("returning error node '"+t+"' @index="+input.index());
			return t;
		}
	};

	public static void main(String[] args) throws Exception {
		CharStream input = null;
		input = new ANTLRFileStream("src/main/java/samples/CompositeModel.java");
		// Create lexer/parser to build trees from stdin
		ModelLexer lex = new ModelLexer(input);
		CommonTokenStream tokens = new CommonTokenStream(lex);
		ModelParser p = new ModelParser(tokens);
		p.setTreeAdaptor(modelAdaptor);
		RuleReturnScope r = p.compilationUnit(); // launch parser by calling
													// start rule
		CommonTree t = (CommonTree) r.getTree(); // get tree result
		//System.out.println("tree: "+t.toStringTree());
		//DOTTreeGenerator dot = new DOTTreeGenerator();
		//System.out.println(dot.toDOT(t));

		CommonTreeNodeStream nodes = new CommonTreeNodeStream(modelAdaptor, t);
		nodes.setTokenStream(tokens);
		SymbolTable symtab = new SymbolTable(); // init symbol table
		ModelDef def = new ModelDef(nodes, symtab); // create Def phase
		def.downup(t); // Do pass 1
		System.out.println("globals: " + symtab.globals);
		nodes.reset(); // rewind AST node stream to root
		ModelRef ref = new ModelRef(nodes); // create Ref phase
		ref.downup(t); // Do pass 2
	}
}
